package cn.hxzy.company.service.impl;

import cn.hxzy.company.dao.UserDao;
import cn.hxzy.company.entity.News;
import cn.hxzy.company.entity.User;
import cn.hxzy.company.entity.vo.PageBean;
import cn.hxzy.company.service.UserService;
import cn.hxzy.company.util.Md5Util;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2020-08-03 22:29:57
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    /**
     * 页面分页
     *
     * @param pageBean
     */
    @Override
    public void queryPage(PageBean pageBean) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<User> users = userDao.queryAllByLimit((page - 1) * size, size);
        pageBean.setList(users);
        long count = userDao.countAll();
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    /**
     * 查询信息
     *
     * @return
     */
    @Override
    public List<User> queryAll() {
        return userDao.findAll();
    }

    /**
     * 查询信息->姓名关键词
     *
     * @param pageBean
     * @param
     */
    @Override
    public void queryPageWithName(PageBean pageBean, String name) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<User> users = userDao.queryAllByLimitWithName("%" + name + "%", (page - 1) * size, size);
        pageBean.setList(users);
        long count = userDao.countAllWithName("%" + name + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    /**
     * 查询信息->id关键词
     *
     * @param pageBean
     * @param
     */
    @Override
    public void queryPageWithId(PageBean pageBean, String id) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<User> users = userDao.queryAllByLimitWithId("%" + id + "%", (page - 1) * size, size);
        pageBean.setList(users);
        long count = userDao.countAllWithId("%" + id + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    @Override
    public User login(User user) {
        String password = user.getPassword();
        String s = Md5Util.MD5(password);
        String name = user.getName();
        User user1 = userDao.LoginWithName(name);
        if (user1 != null&& user1.getPassword().equals(s)) {
            return user;
        }
        return null;
    }


}